<template>
  <div class="top-box">
    <div style="cursor: pointer" class="fs-6" @click="router.push('/news')">&lt;</div>
    <div class="fs-6">{{ news.title }}</div>
    <div>
      <HuUserInfo></HuUserInfo>
    </div>
  </div>

  <div class="news">
    <div v-if="news.id">
      {{ news.source }}
      {{ Tools.formatDate(news.createdAt, 'yyyy-MM-dd') }}
      浏览量：{{ news.hits }} 点赞数：{{ news.likes }}
    </div>
    <div v-else>
      <NEmpty description="新闻信息获取中。。。"></NEmpty>
    </div>
    <div v-html="news.content"></div>
  </div>
  <NDivider>评论</NDivider>
  <div v-if="list.length == 0">
    <NEmpty description="暂无评论"></NEmpty>
    <div class="tc">
      <NButton @click="showAdd" type="primary">抢先发布</NButton>
    </div>
  </div>
  <div v-else>
    <div class="tc p-2">
      <NButton @click="showAdd" type="primary">发布评论</NButton>
    </div>

    <div class="replies">
      <div v-for="d in list" :key="d.id">
        <div class="content">
          <div>
            <NAvatar round :src="Api.userlogo(d.userId)"></NAvatar>
          </div>
          <div>
            <div>
              {{ d.user.info.nickname || d.user.user.username }}
              {{ Tools.formatDate(d.createdAt) }}
              - <span @click="praiseReply(d)" :class="{ active: d.praised }">赞</span>
              <span>({{ d.likes }})</span>
            </div>
            <div>
              {{ d.content }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 发布评论的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="visible" title="发布评论">
      <div>
        <NInput type="textarea" v-model:value="content" placeholder="评论内容"></NInput>
      </div>
      <template #action>
        <NButton type="primary" @click="addReply">发布</NButton>
      </template>
    </NModal>
  </div>
</template>

<script setup lang="ts">
import { Api } from '@/types/Api'
import type { BaseDataResult, BaseResult, PageBean, PageVO } from '@/types/BaseResult'
import type { TbDataNews, TbDataNewsReply } from '@/types/ThirdApi'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { NAvatar, NButton, NDivider, NEmpty, NInput, NModal, useMessage } from 'naive-ui'
import HuUserInfo from '@/components/HuUserInfo.vue'
import { Tools } from '@/types/Tools'
const message = useMessage()

const router = useRouter()
const route = useRoute()

const id = route.params.id
console.log('新闻id：', id)

const loading = ref(false)

//#region 新闻详情
const news = ref({} as TbDataNews)
const query = () => {
  loading.value = true
  Api.get(`/autodata/query/newsdetail/${id}`, {}, (data: BaseDataResult<TbDataNews>) => {
    loading.value = false
    if (data.success) {
      news.value = data.data
      queryReply()
      return
    }
    message.error(data.message)
  })
}
//#endregion

//#region 新闻评论
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as Array<TbDataNewsReply>)

const queryReply = () => {
  loading.value = true
  Api.get(
    `/autodata/query/newsreplies/${id}/${page.value.pageSize}/${page.value.pageNumber}`,
    {},
    (data: BaseDataResult<PageVO<TbDataNewsReply>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

const praiseReply = (info: TbDataNewsReply) => {
  Api.post(`/autodata/info/news/reply/like/${info.id}`, {}, (data: BaseDataResult<number>) => {
    if (data.success) {
      info.likes = data.data
      info.praised = !info.praised
      return
    }
    message.error(data.message)
  })
}

//#endregion

//#region 发布评论
const visible = ref(false)
const content = ref('')
const showAdd = () => {
  content.value = ''
  visible.value = true
}

const addReply = () => {
  loading.value = true
  Api.post(
    `/autodata/info/news/reply/${id}`,
    {
      content: content.value,
    },
    (data: BaseResult) => {
      loading.value = false
      if (data.success) {
        visible.value = false
        message.success(data.message)
      } else {
        message.error(data.message)
      }
    },
  )
}

//#endregion

onMounted(() => {
  query()
})
</script>

<style scoped lang="scss">
.top-box {
  display: flex;
  padding: 1rem;
  justify-content: space-between;
}

.news {
  width: 50vw;
  margin: 0px auto;
  :deep(p) {
    font-size: 1.2rem;
    text-indent: 2em;
  }

  > div:first-of-type {
    font-size: 1.2rem;
  }
}

.replies {
  width: 50vw;
  margin: 0px auto;
  > div {
    margin-bottom: 0.5rem;
  }
  .content {
    display: flex;
    align-items: flex-start;
    > div:last-of-type {
      flex: 1;
      margin-left: 0.5rem;
    }
    .active {
      color: #ff0000;
    }
  }
}
</style>
